package com.jason.frescotest.adapter;

import android.content.Context;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

import com.facebook.drawee.backends.pipeline.Fresco;
import com.facebook.drawee.drawable.ProgressBarDrawable;
import com.facebook.drawee.generic.GenericDraweeHierarchy;
import com.facebook.drawee.interfaces.DraweeController;
import com.facebook.drawee.view.SimpleDraweeView;
import com.facebook.imagepipeline.request.ImageRequest;
import com.facebook.imagepipeline.request.ImageRequestBuilder;
import com.jason.frescotest.R;
import com.jason.frescotest.bean.ImageData;

import java.util.List;

/**
 * @Author: Jason
 * @Date: 15/12/10
 * @Time: 18:39
 */

public class GifAdapter extends BaseAdapter {


    private List<ImageData> mImageDataList;
    private Context mContext;

    public GifAdapter(List<ImageData> list, Context context) {
        this.mImageDataList = list;
        this.mContext = context;
    }

    @Override
    public int getCount() {
        return mImageDataList.size();
    }

    @Override
    public Object getItem(int position) {
        return mImageDataList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        ViewHolder viewHolder = null;

        if (convertView == null) {

            viewHolder = new ViewHolder();
            convertView = LayoutInflater.from(mContext).inflate(R.layout.list_item_layout, parent, false);
            viewHolder.simple_draweeview = (SimpleDraweeView) convertView.findViewById(R.id.simple_draweeview);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }


        Uri uri;
        ImageData imageData = mImageDataList.get(position);
        GenericDraweeHierarchy hierarchy;

        if (imageData.type == 0) {
            uri = Uri.parse(imageData.url);
            viewHolder.simple_draweeview.setImageURI(uri);
            hierarchy = viewHolder.simple_draweeview.getHierarchy();
            hierarchy.setProgressBarImage(new ProgressBarDrawable());

        } else {
            uri = Uri.parse(imageData.url);
            ImageRequest request = ImageRequestBuilder.newBuilderWithSource(uri).build();
            DraweeController controller = Fresco.newDraweeControllerBuilder()
                    .setImageRequest(request)
                    .setAutoPlayAnimations(true)
                    .build();
            viewHolder.simple_draweeview.setController(controller);
            hierarchy = viewHolder.simple_draweeview.getHierarchy();
            hierarchy.setProgressBarImage(new ProgressBarDrawable());

        }




        return convertView;
    }


    private class ViewHolder {


        public SimpleDraweeView simple_draweeview;

    }



}
